package com.wlyuan.open.controller;

import com.wlyuan.basic.api.ICarrierService;
import com.wlyuan.open.annotation.OpenApi;
import com.wlyuan.open.assembler.CarrierAssembler;
import com.wlyuan.open.basic.dto.OpenCarrierCreationDTO;
import com.wlyuan.open.basic.dto.OpenCarrierUpdateDTO;
import com.wlyuan.open.basic.vo.OpenCarrierDetailVO;
import com.wlyuan.open.translator.CarrierTranslator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.var;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author yuanjie
 */
@Api(tags = "承运方")
@Validated
@RequestMapping("/api/carrier")
@RestController
public class OpenCarrierController {
    @DubboReference(check = false)
    private ICarrierService carrierService;

    @ApiOperation("创建线下承运商")
    @PostMapping("/offline")
    @OpenApi
    public OpenCarrierDetailVO create(@RequestBody @Valid OpenCarrierCreationDTO openCreation) {
        var offlineCarrier = CarrierAssembler.assemble(openCreation);
        return CarrierTranslator.translate(carrierService.create(offlineCarrier));
    }

    @ApiOperation("修改线下承运商")
    @PutMapping("/offline")
    @OpenApi
    public OpenCarrierDetailVO update(@RequestBody @Valid OpenCarrierUpdateDTO openCarrier) {
        var offlineCarrier = CarrierAssembler.assemble(openCarrier);
        var detail = carrierService.update(offlineCarrier);
        return CarrierTranslator.translate(detail);
    }


}
